<template>
  <div class="container">
    <div class="title">
      <span>修改图书</span>
      <span class="back" @click="back"> <i class="iconfont icon-fanhui"></i> 返回 </span>
    </div>
    <el-divider></el-divider>
    <div class="wrap">
      <el-row>
        <el-col :lg="16" :md="20" :sm="24" :xs="24">
          <el-form
            :model="form"
            status-icon
            ref="form"
            label-width="100px"
            v-loading="loading"
            @submit.native.prevent
          >
            <el-form-item label="模板名称" prop="title">
              <el-input
                size="medium"
                v-model="form.title"
                placeholder="请填写模板名称"
              ></el-input>
            </el-form-item>
            <el-form-item label="作者" prop="author">
              <el-input
                size="medium"
                v-model="form.author"
                placeholder="请填写作者"
              ></el-input>
            </el-form-item>
            <el-form-item label="封面" prop="image">
              <el-input
                size="medium"
                v-model="form.image"
                placeholder="请填写封面地址"
              ></el-input>
            </el-form-item>
            <el-form-item label="普通示例">
              <upload-imgs ref="uploadEle3" :rules="rules" :multiple="true" />
              <div>
                <el-button @click="getValue('uploadEle3')">获取当前图像数据</el-button>
              </div>
            </el-form-item>
            <el-form-item label="文章内容">
              <tinymce
                :defaultContent="data"
                @change="change"
                upload_url="http://dev.lin.colorful3.com/cms/file/"
              />
            </el-form-item>
            <el-form-item class="submit">
              <el-button type="primary" @click="submitForm('form')">保 存</el-button>
              <el-button @click="resetForm('form')">重 置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import book from "@/model/book";
import Tinymce from "@/component/base/tinymce";
import UploadImgs from "@/component/base/upload-image";
export default {
  components: {
    Tinymce,
    UploadImgs,
  },
  props: {
    editBookID: {
      type: Number,
    },
  },
  data() {
    return {
      data: "",
      rules: {
        minWidth: 100,
        minHeight: 100,
        maxSize: 5,
      },
      loading: false,
      form: {
        title: "",
        author: "",
        summary: "",
        image: "",
      },
    };
  },

  async mounted() {
    this.loading = true;
    // this.form = await book.getBook(this.editBookID);
    this.loading = false;
  },
  methods: {
    change(val) {
      this.data = val;
    },
    async submitForm() {
      const res = await book.editBook(this.editBookID, this.form);
      if (res.code < window.MAX_SUCCESS_CODE) {
        this.$message.success(`${res.message}`);
        this.$emit("editClose");
      }
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    back() {
      this.$emit("editClose");
    },
  },
};
</script>

<style lang="scss" scoped>
.el-divider--horizontal {
  margin: 0;
}

.container {
  .title {
    height: 59px;
    line-height: 59px;
    color: $parent-title-color;
    font-size: 16px;
    font-weight: 500;
    text-indent: 40px;

    .back {
      float: right;
      margin-right: 40px;
      cursor: pointer;
    }
  }

  .wrap {
    padding: 20px;
  }

  .submit {
    float: left;
  }
}
</style>
